<?php


namespace app\zhengmao\controller;



use app\zhengmao\model\GoodsModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

class GoodsController extends AdminBaseController
{
    //显示首页
    public function index(){
        //获取查询数据
        $name = input('title');
        $where = [];
        //判断是否有值
        if($name){
            $where[] = ['title','like','%'.$name.'%'];
        }
        //查询排序分页
        $list = GoodsModel::order('id DESC')
            ->where($where)
            ->paginate(10);
        return $this->fetch('index',[
            'list' => $list,
        ]);
    }
    //添加
    public function add()
    {
        $list = GoodsModel::select();
        return $this->fetch('add',[
            'list' => $list,
        ]);
    }
    public function addPost()
    {
        $data = input('post.');
        $data['images'] = implode(',', $data['images']);
        $data['create'] = time();
        if(GoodsModel::create($data)){
            $this->success('添加成功','Goods/index');
        }else{
            $this->error('添加失败');
        }
    }
    //修改
    public function edit()

    {
        $id = input('id');
        $list = GoodsModel::find($id);
        return $this->fetch('edit',[
            'list' => $list,
        ]);
    }
    public function editPost()
    {
        $data = input('post.');
        //查询是否有此记录
        $list = GoodsModel::find($data['id']);
        if(!$list){
            $this->error('未查询到该记录');
        }
        if($list->update($data)){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }
    //删除
    public function delete()
    {
        $id = input('id/d','');
        $del = GoodsModel::destroy($id);
        if($del){
            $this->success('删除成功','Goods/index');
        }else{
            $this->error('删除失败');
        }
    }
    //显示隐藏内容
    public function isShow(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            $this->error('未查询到该id');
        }
        $goods = GoodsModel::find($id);
        $source = null;
        if($goods['self_status'] == 1){
            $source = 0;
        }
        if($goods['self_status'] == 0){
            $source = 1;
        }
        if(GoodsModel::update(['self_status'=>$source],['id'=>$id])){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }

    // 打开配置页面
    public function setGoodsAttr()
    {
        $goodsId = input('goods_id');
        $list = \think\facade\Db::name('goods_details')
            ->where('goods_id', $goodsId)
            ->select();
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr', ['list' => $list]);
    }
    // 配置提交
    public function setGoodsAttrPost() {
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs)-1 < 1){
            $this->error('请至少配置一个属性！');
        }
        $insert_data = [];
        foreach($attrs as $attr){
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr
            ];
        }
        \think\facade\Db::name('goods_details')->insertAll($insert_data);
        $this->success('保存成功');
    }

    public function setGoodsAttrValue()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr_value');
    }
    public function GetGoodsAttrValue()
    {
        $goodsId = input('goods_id');

        $attrs = \think\facade\Db::name('goods_details')
            ->where('goods_id', $goodsId)
            ->select();

        return json(['code'=>1,'msg'=>'商品属性','data'=>$attrs]);
    }
    public function setGoodsAttrValuePost()
    {
        $data = input('post.form');

        $insert_data = [];
        foreach($data as $attr){
            $insert_data[] = [
                'value' => $attr['attr_value'],
                'details_id' => $attr['attr']
            ];
        }
        $insert = \think\facade\Db::name('goods_value')->insertAll($insert_data);
        if ($insert){
            return json(['code'=>1,'msg'=>'添加成功','data'=>"NULL"]);
        }else{
            return json(['code'=>0,'msg'=>'添加失败','data'=>"NULL"]);
        }

    }
}